package com.example.xgakj.newterminal.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.beans.DeviceBean;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.socks.library.KLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MonitorManageActivity extends BaseActivity {

    @BindView(R.id.list_video)
    ListView listVideo;
    @BindView(R.id.list_police)
    ListView listPolice;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    private String[] mTitles1;
    private String[] mTitles2;
    private int[] icons1;
    private int ifaddr=-1;
    private String deviceAddress;
    private String devicePwd;

    private DeviceBean mDevice;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor_manage);
        ButterKnife.bind(this);
        initTopbar();
        mTitles1 = new String[]{"视频回放查询","截图查看"};
        mTitles2 = new String[]{"布防、撤防记录查询", "报警记录查询"};
        icons1 = new int[]{R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round};
        listVideo.setAdapter(new MonitorAdapter(mTitles1, icons1));
        listPolice.setAdapter(new MonitorAdapter(mTitles2, icons1));
        initListner();
        //注册接收登录查询到的用户设备
        EventBus.getDefault().register(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onQueryDevice(DeviceBean device){
        deviceAddress = device.getAddress();
        devicePwd = device.getPwd();
        KLog.e(deviceAddress +"*"+ devicePwd);
        ifaddr = device.getIfaddr();
        mDevice=device;

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initTopbar() {
        topbar.setTitle("查询管理");
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initListner() {
        listVideo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                switch (position) {
                    case 1:
                        Intent intent2=new Intent(MonitorManageActivity.this, ScreenShotsActivity.class);
                        startActivity(intent2);
                        break;
                    default:
                       Intent intent3=new Intent(MonitorManageActivity.this, VideoRecordListActivity.class);
                       startActivity(intent3);
                        break;
                }
            }
        });
        listPolice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MonitorManageActivity.this, AlarmRecordActivity.class);
                switch (position) {
                    case 0:
                        intent.putExtra(Parameters.TYPE, Parameters.TYPE_DEFENCE);
                        startActivity(intent);
                        break;
                    case 1:
                        intent.putExtra(Parameters.TYPE, Parameters.TYPE_ALARM);
                        startActivity(intent);
                        break;

                    default:

                }
            }
        });

    }

    private class MonitorAdapter extends BaseAdapter {
        private String[] titles;
        private int[] icons;

        public MonitorAdapter(String[] mTitles, int[] icons) {
            this.titles = mTitles;
            this.icons = icons;
        }


        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public String getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_set_list, null);
            TextView textView = view.findViewById(R.id.tv_name);
            ImageView imageView =  view.findViewById(R.id.iv_leftIcon);
            textView.setText(titles[position]);
            return view;
        }
    }

}
